package com.aqgj.common;

import com.ztc.util.exceptions.AssemblingSQLException;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Table;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public final class DaoUtil {
    public static Map<String,String> updateAssembling(Object object) throws InvocationTargetException, IllegalAccessException {
        Map<String,String> linkedHashMapMap = new LinkedHashMap<>();
        Class objectClass = object.getClass();
        Table table = (Table) objectClass.getAnnotation(Table.class);
        if(table==null){
            throw new AssemblingSQLException("该类不是实体类");
        }
        String tableName = table.name();
        if(tableName==null){
            tableName = objectClass.getSimpleName().toLowerCase();
        }
        Method[] methods = objectClass.getDeclaredMethods();
        StringBuffer sb=new StringBuffer();
        for(int i = 0; i < methods.length; i++){
            Method method = methods[i];
            if(method.getName().startsWith("get")){
                if(method.invoke(object)!=null && method.getReturnType()!=Set.class && method.getReturnType()!=List.class){
                    Class returnType = method.getReturnType();
                    Id id = method.getAnnotation(Id.class);
                    if(id!=null){
                        continue;
                    }
                    Column column = method.getAnnotation(Column.class);
                    JoinColumn joinColumn = method.getAnnotation(JoinColumn.class);
                    if(column!=null && joinColumn!=null){
                        throw new AssemblingSQLException("column 与 joinColumn 不能同时存在");
                    }
                    String name="";
                    if(column!=null){
                        name=column.name();
                        sb.append(column.name()).append("= ? ,");
                    }
                    if(joinColumn!=null){
                        name=joinColumn.name();
                        sb.append(joinColumn.name()).append("= ? ,");
                    }
                    if(column==null && joinColumn==null){
                        sb.append(method.getName().substring(3,4).toLowerCase()).append(method.getName().substring(4,method.getName().length())).append("= ? ,");
                    }
                    if(returnType==String.class){
                        linkedHashMapMap.put(":"+name,method.invoke(object).toString());
                    }else if(returnType==Date.class){
                        Date d=(Date) method.invoke(object);
                        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                        String str = df.format(d);
                        linkedHashMapMap.put(":"+name,str);

                    }else{
                        linkedHashMapMap.put(":"+name,method.invoke(object).toString());
                    }
                }
            }
        }
        String sql =  "update " + tableName + " set "+sb.substring(0,sb.length()-1);
        linkedHashMapMap.put("sql",sql);
        return linkedHashMapMap;
    }
}
